/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mygameframework.isoengine;

import java.awt.Dimension;
import java.awt.Point;

/**
 *
 * @author bill
 */
public class TilePlotter {
    private MapType mapType;
    private int tileWidth;
    private int tileHeight;

    public TilePlotter(MapType mapType, int tileWidth, int tileHeight) {
        this.mapType = mapType;
        this.tileWidth = tileWidth;
        this.tileHeight = tileHeight;
    }

    
    public MapType getMapType() {
        return mapType;
    }

    public void setMapType(MapType mapType) {
        this.mapType = mapType;
    }

    public TilePlotter() {
        this.mapType=MapType.MAP_RECTANGULAR;
        this.tileWidth=1;
        this.tileHeight=1;
    }
    public void setTileSize(int tw,int th){
        this.tileWidth=tw;
        this.tileHeight=th;
    }
    public Dimension getTileSize(){
        return new Dimension(tileWidth, tileHeight);
    }

    public int getTileHeight() {
        return tileHeight;
    }

    public int getTileWidth() {
        return tileWidth;
    }
    /***
     * convert the point from map to world
     * @param ptMap 地图坐标
     * @return WorldSpace坐标
     */
    public Point plotTile(Point ptMap){
        Point result;
        switch (mapType) {
            case MAP_SLIDE:
                result=slidPlotTile(ptMap, tileWidth, tileHeight);
                break;
            case MAP_STAGGERED:
                result=staggeredPlotTile(ptMap, tileWidth, tileHeight);
                break;
            case MAP_DIAMOND:
                result=diamondPlotTile(ptMap, tileWidth, tileHeight);
                break;
            case MAP_RECTANGULAR:
                result=rectanglePlotTile(ptMap, tileWidth, tileHeight);
                break;
                
            default:
                throw new AssertionError();
        }
        return result;
    }
    protected Point slidPlotTile(Point ptMap,int tw,int th){
        Point result=new Point();
	//calculate pixel position for the map position given
	result.x=ptMap.x*tw+ptMap.y*(tw>>1);
	result.y=ptMap.y*(th>>1);
//        result.y+=offsety;
//        result.x+=offsetx;
	//return calculate point
	return result;
    }
    protected Point staggeredPlotTile(Point ptMap,int tw,int th){
        Point result=new Point();
	result.x=ptMap.x*tw+ (ptMap.y&1)*(tw>>1);
	result.y=ptMap.y*(th>>1);
//        result.y+=offsety;
//        result.x+=offsetx;
	//return calculate point
	return result;
    }
    protected Point diamondPlotTile(Point ptMap,int tw,int th){
        Point result=new Point();
	//calculate pixel position for the map position given
	result.x=(ptMap.x-ptMap.y)*(tw>>1); //tw/2
	result.y=(ptMap.y+ptMap.x)*(th>>1);
//        result.y+=offsety;
//        result.x+=offsetx;
	//return calculate point
	return result;
    }
    protected Point rectanglePlotTile(Point ptMap,int tw,int th){
        Point result=new Point();
        result.x=ptMap.x*tw;
        result.y=ptMap.y*th;
        return result;
    }
}
